package co.fitstart.mobile.dao.support;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.entity.user.agent.AgentRecord;
import co.fitstart.entity.user.agent.OfflineAgent;

@Repository("agentDaoSupport")
public class AgentDaoSupport {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * 
     * @param discountCode
     * @return
     */
    public AgentDiscount loadAgentDiscount(String discountCode) {
        return genericDaoSupport.searchForObject("FROM AgentDiscount WHERE disabled = false AND discountCode = :discountCode", "discountCode", discountCode, AgentDiscount.class);
    }
    
    /**
     * 
     * @param discountCode
     * @return
     */
    public AgentDiscount loadDiscount(String discountCode) {
        return genericDaoSupport.searchForObject("FROM AgentDiscount WHERE discountCode = :discountCode", "discountCode", discountCode, AgentDiscount.class);
    }
    
    /**
     * 
     * @param user
     * @return
     */
    public Agent loadAgent(User user) {
        return genericDaoSupport.searchForObject("FROM Agent agent WHERE agent.disabled = false AND agent.user = :user", "user", user, Agent.class);
    }
    
    /**
     * 
     * @param agent
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AgentDiscount> listAgentDiscount(Agent agent) {
        return genericDaoSupport.searchForList("FROM AgentDiscount WHERE disabled = false AND agent = :agent", "agent", agent);
    }
    
    /**
     * 
     * @param agent
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<AgentRecord> listAgentRecord(Agent agent) {
        return genericDaoSupport.searchForList("FROM AgentRecord WHERE disabled = false AND agent = :agent ORDER BY occurTime DESC", "agent", agent);
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<OfflineAgent> listOfflineAgent(Student student) {
        return genericDaoSupport.searchForList("FROM OfflineAgent WHERE disabled = false AND courseRound.student = :student ORDER BY createTime DESC", "student", student);
    }
 
}
